package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.pb.activity.unionwar.UnionWarActivityServerProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.List;

@Cmd(code = Protocol.C_CROSS_UNION_WAR_REQUEST_BATTLE_RESULT, desc = "请求跨服战斗数据")
public class RequestCrossBattleDataReqCmd extends CrossUnionCommand {


    private int activityId;
    private int phase;
    private int session;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        UnionWarActivityServerProto.RequestCrossBattleDataReqMsg reqMsg = UnionWarActivityServerProto.RequestCrossBattleDataReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        phase = reqMsg.getPhase();
        session = reqMsg.getSession();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = CrossUnionWarActivityMgr.getOpenActivity(activityId);
        if(null == activityInfo){
            return;
        }
        //根据区服拿分组
        List<Integer> groupIdList = CrossUnionWarMgr.getGroupListByServerId(activityId, serverId);

        for (int groupId : groupIdList) {
            CrossUnionWarProto.CrossUnionWarBattleResultSyncMsg.Builder syncMsg = CrossUnionWarMgr.getUnionWarBattleResultMsg(activityId, phase, session, groupId);
            if(null == syncMsg){
                return;
            }
            CrossUnionWarProto.CrossUnionWarBattleResultSyncMsg.Builder tempMsg = CrossUnionWarMgr.filterUnionWarBattleResultMsg(serverId, syncMsg);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_UNION_WAR_RESULT_SYNC_FORM_CROSS, tempMsg);
            MessageHelper.sendPacket(serverId, 0, pbMsg);
        }
    }

    @Override
    public void sendData() {

    }
}
